import { getBaseUrl, requestUtil } from '../../utils/requestUtil.js'
import regeneratorRuntime from "../../lib/runtime/runtime"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [],
    baseUrl: '',
    bigTypeList: [],
    bigTypeList_row1: [],
    bigTypeList_row2: [],
    hotProductList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const baseUrl = getBaseUrl();
    this.setData({
      baseUrl
    })

    this.getSwiperList()
    this.getBigTypeList() 
    this.getHotProductList() 
  },

  handleTypeJump(e) {
    const { index } = e.currentTarget.dataset

    const app = getApp()
    app.globalData.index = index

    wx.switchTab({
      url: '/pages/category/index'
    })
  },

  async getSwiperList() {
    const result = await requestUtil({
      url: '/product/findSwiper',
      method: "GET"
    });
    this.setData({
      swiperList: result.message
    })
  },

  async getHotProductList() {
    const result = await requestUtil({
      url: '/product/findHot',
      method: "GET"
    });
    this.setData({
      hotProductList: result.message
    })
  },

  async getBigTypeList() {
    const result = await requestUtil({
      url: '/bigType/findAll',
      method: "GET"
    });
    const bigTypeList = result.message
    const bigTypeList_row1 = bigTypeList.filter((item, index) => {
      return index < 5
    })
    const bigTypeList_row2 = bigTypeList.filter((item, index) => {
      return index >= 5
    })
    this.setData({
      bigTypeList,
      bigTypeList_row1,
      bigTypeList_row2,
    })
  }
})